<?php

class Whitelist {
	private $message = "";
	private $auth = null;
	function Whitelist() {
	}

	public function run() {
		$p = '/@whitelist[\s]+((<-)|(->))([0-9]{4,15})[\s]*/si';
		preg_match($p, trim($_POST[Message]), $n);
		if ("<-" == $n[1]) {
			$this->addWhitelist($n);
		} else
			if ("->" == $n[1]) {
				$this->delWhitelist($n);
			} else {
				$this->showWhitelist();
			}

	}
	private function addWhitelist($n) {
		$qq = $n[4];
		$update_time = date("Y-m-d H:i:s", (time() + 8 * 3600));
		$ret = @ DB :: GetQueryResult("select * from td_whitelist where qq='{$qq}' and clusternum='{$_POST[ClusterNum]}'", true);
		if (!$n[4]) {
			$this->message = "无效的qq号码~！";
			return;
		}
		


		if ($ret) {
			$this->message = "该号码已经在白名单里面啦-。-";
			return;
		} else {
			$arr = array (
				'qq' => $qq,
				'clusternum' => $_POST[ClusterNum],
				'update_time' => $update_time
			);
			DB :: SaveTableRow('td_whitelist', $arr);
			$this->message = "添加成功, 白名单里的成员将不检测群设置的filter以免误踢成员";
		}

	}
	private function delWhitelist($n) {
		$qq = $n[4];

				$sql1 = "select * from td_whitelist where qq='{$qq}' and clusternum='{$_POST[ClusterNum]}'";
				$sql2 = "delete from  td_whitelist  where qq='{$qq}' and clusternum='{$_POST[ClusterNum]}' ";
			
		$ret = @ DB :: GetQueryResult($sql1, true);
		if (!$n[4]) {
			$this->message = "无效的qq号码~！";
			return;
		}
		
		if ($ret) {
			
			@ DB :: Query($sql2);
			$this->message = "移除成功~！- -";

		} else {
			$this->message = "该用户还不在本群白名单里面";
		}
		return;
	}
	private function showWhitelist() {
		$ret2 = @ DB :: GetQueryResult("select * from td_whitelist where    clusternum='{$_POST[ClusterNum]}'", false); //配置参数初始化
		$mess = "";
		$count = count($ret2);
		if ($count != 0) {
			$mess .= "本群白名单里面共有{$count}人,分别为：\n";
			foreach ($ret2 as $key => $v) {
				$mess .= "qq号码：{$v[qq]} \t添加日期：{$v[update_time]}\n";
			}
		} else {
			$mess .= "本群白名单里目前还没有任何人\n";
		}
		$this->message = $mess . "\n" .
		"使用下列命令来添加和删除白名单成员：\n" .
		"@whitelist <-qq号码              添加qq号码到白名单   \n" .
		"@whitelist ->qq号码             把该qq从本群的白名单移除    \n" .
		"例子：\n" .
		"@whitelist <-261589289     意思就是吧261589289这个用户放入本群白名单\n" .
		"@whitelist ->261589289     意思就是吧261589289这个用户从白名单中移除";
	}
	
	public function isInWhitelist() {

		$sql = "select * from td_whitelist where  qq={$_POST[Sender]} and clusternum ='{$_POST[ClusterNum]}' ";
		$ret2 = @ DB :: GetQueryResult($sql, true);
		if ($ret2)
			return true;
		else
			return false;

	}
	
	private function __get($property_name) {
		return $this-> $property_name;
	}

	private function __set($property_name, $value) {
		$this->$property_name = $value;
	}

}
?>